package com.hopu.controller;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.hopu.pojo.Mydept;
import com.hopu.service.DeptService;
import com.hopu.util.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/emp")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 查询全部，分页
     * @param pn
     * @param pflag
     * @return
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public ResultUtils findAll(@RequestParam(value = "pn", defaultValue = "1") Integer pn, @RequestParam(value = "pflag", defaultValue = "1") Integer pflag) {
        PageHelper.startPage(pn, 14);
        List<Mydept> all = deptService.findAll(pflag);
        PageInfo page = new PageInfo(all, 5);
        return new ResultUtils(200, "yeah", page);
    }
    /**
     * 添加
     * @param mydept
     * @return
     */

    @RequestMapping("/insert")
    @ResponseBody
    public ResultUtils insert(Mydept mydept){
        System.out.println(mydept);
        deptService.insert(mydept);
        return new ResultUtils(20,"yeah",null);
    }

    /**
     * 查询名字重复的方法
     * @param pname
     * @return
     */
    @RequestMapping("/findByname")
    @ResponseBody
    public ResultUtils findByname(@RequestParam String pname){
        List<Mydept> byname = deptService.findByname();
        int i =1;
        for (Mydept mydept : byname) {
            System.out.println(byname.size()+"                   "+ i+++"                    "+mydept.getPname().equals(pname));
            if (mydept.getPname().equals(pname)){
                System.out.println(mydept.getPname()+"---------------------"+pname+"------------------"+mydept.getPname().equals(pname));
                return new ResultUtils(200,"no",null);
            }
        }
    return new ResultUtils(20,"yeah",null);

    }
    @RequestMapping("/falsedelete")
    @ResponseBody
    public ResultUtils falsedelete(@RequestParam Integer pid,@RequestParam Integer pflag){
        System.out.println(pflag);
        if (pflag==1){
            pflag =0;
            System.out.println("if         "+pflag);
            deptService.falsedelete(pid,pflag);
            return new ResultUtils(200,"yeah",null);
        }
        pflag =1;
        System.out.println("else         "+pflag);
        deptService.falsedelete(pid,pflag);
        return new ResultUtils(200,"yeah",null);
    }
    @RequestMapping("/updatePart")
    @ResponseBody
    public ResultUtils updatePart(Mydept mydept){
        System.out.println(mydept);
        deptService.updatePart(mydept);
        return new ResultUtils(200,"yeah",null);
    }

    @RequestMapping("/findById")
    @ResponseBody
    public ResultUtils findById(@RequestParam Integer pid){
        Mydept byId = deptService.findById(pid);
        return new ResultUtils(200,"yeah",byId);
    }

}
